package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.miaodong.entity.AppDynamicDianzan;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static com.vf.admin.utils.StringEx.isNull;

/**
 * 动态点赞
 */
@Repository
public class AppDynamicDianzanDao extends BaseDao<AppDynamicDianzan> {
    /**
     * 获取指定用户&指定动态获取点赞信息
     *
     * @param userid    用户id
     * @param dynamicid 动态id
     * @return
     */
    public AppDynamicDianzan getSpecifiedLike(Integer userid, Integer dynamicid) {
        if(userid==null || userid<=0 || dynamicid==null || dynamicid<=0 ){
            return null;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_dynamic_dianzan where userid = ? and dynamicid = ?   ");
        paramList.add(userid);
        paramList.add(dynamicid);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppDynamicDianzan.class);
    }
    /**
     * 获取指定用户&指定动态获取点赞信息
     *
     * @param userid    用户id
     * @param arr 动态id 拼接
     * @return
     */
    public List<AppDynamicDianzan> getPiliang(Integer userid, String arr) {
        if(userid==null || userid<=0){
            return null;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_dynamic_dianzan where userid = ?  ");
        paramList.add(userid);
        if(!isNull(arr)) {
            sbStr.append(" and dynamicid in ( ");
            sbStr.append(arr);
            sbStr.append(" ) ");
        }
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppDynamicDianzan.class);
    }

    /**
     * 详情
     */
    public AppDynamicDianzan get(Integer id) {
        if (id==null || id<=0) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_dynamic_dianzan where id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppDynamicDianzan.class);
    }


    public List<AppDynamicDianzan> count(Integer id) {
        if (id==null || id<=0) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_dynamic_dianzan where dynamicid = ? order by createtime desc");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppDynamicDianzan.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppDynamicDianzan haoYou) {
        if (haoYou!=null && haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id==null || id <= 0) {
            return;
        }
        String sql = "delete  from app_dynamic_dianzan where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete  from app_dynamic_dianzan where userid = "+userId;
        this.del(sql, new ArrayList<>());
    }

    /**
     * 更新用户头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(userid==null || userid<=0 || isNull(pic)){
            return ;
        }
        String sql = " UPDATE app_dynamic_dianzan SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新用户昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(userid==null || userid<=0 || isNull(nick)){
            return ;
        }
        String sql = " UPDATE app_dynamic_dianzan SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }
}
